const Service = require('node-windows').Service;
const path = require('path');

// 创建一个新的服务对象
const svc = new Service({
  name: 'LogServer',
  description: 'Log Server WebSocket Service',
  script: path.join(__dirname, 'server.js')
});

// 监听卸载事件
svc.on('uninstall', function() {
  console.log('Log Server service uninstalled successfully!');
  console.log('Service has been removed from Windows services.');
});

// 监听停止事件
svc.on('stop', function() {
  console.log('Log Server service stopped.');
  console.log('Proceeding with uninstallation...');
  svc.uninstall();
});

// 监听错误事件
svc.on('error', function(err) {
  console.error('Service error:', err);
});

// 停止并卸载服务
console.log('Stopping and uninstalling Log Server Windows service...');
if (svc.exists) {
  svc.stop();
} else {
  console.log('Service does not exist, nothing to uninstall.');
}